package cn.tedu.front.job.service.impl;

import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.common.pojo.po.UserStatePO;
import cn.tedu.common.pojo.vo.PageData;
import cn.tedu.front.job.dao.cache.IPositionCacheRepository;

import cn.tedu.front.job.dao.persist.repository.IPositionRepository;
import cn.tedu.front.job.dao.search.PositionSearchRepository;
import cn.tedu.front.job.pojo.dto.PositionAddNewParam;
import cn.tedu.front.job.pojo.dto.PositionUpdateInfoParam;
import cn.tedu.front.job.pojo.entity.Position;
import cn.tedu.front.job.pojo.vo.*;
import cn.tedu.zhaopin.protocol.position.vo.PositionSimpleVO;

import cn.tedu.front.job.service.IPositionService;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PositionServiceImpl implements IPositionService {
    @Autowired
    private IPositionRepository positionRepository;
    @Autowired
    private PositionSearchRepository positionSearchRepository;
    @Autowired
    private IPositionCacheRepository positionCacheRepository;

    /*

     */
    @Override
    public void addNew(Long userId, PositionAddNewParam positionAddNewParam) {
        //根据id找用户,找到用户的公司以及            公司id
        System.out.println("currentp:" + userId+ "-------posiadd:" + positionAddNewParam);


        RecruiterStanderVO recruiterStanderVO = positionRepository.getRecruiterByUserId(userId);
        //判断  无公司 抛错
        if (recruiterStanderVO == null) {
            String message = "发布职位失败,服务器忙,请稍后再尝试！,提交公司信息";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        String companyName = recruiterStanderVO.getCompanyName();
        Long companyId = recruiterStanderVO.getCompanyId();
        //根据职位类型id查找职位 判断没职位抛错
     /*   JobCategoryByIdVO jobCategoryByIdVO = positionRepository.getCategoryById(positionAddNewParam.getPostTypeId());
        if (jobCategoryByIdVO == null) {
            String message = "发布职位失败，服务器忙，请稍后再尝试！,类别不对昂";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }*/
        //发布职位   判断是否添加成功

        Position position = new Position();
        BeanUtils.copyProperties(positionAddNewParam, position);
        position.setCompanyId(companyId);
        position.setUserId(userId);

        Long positionId = positionRepository.addNew(position);

        if (positionId == null) {
            String message = "发布职位失败，服务器忙，请稍后再尝试！插不进去";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        log.info("positionId:" + positionId);
        //存储 职位简单信息到es
        PositionSimpleVO positionSimpleVO = new PositionSimpleVO();
        BeanUtils.copyProperties(position, positionSimpleVO);
        log.info("开始执行向es中存储(修改)职位详情positionSimpleVO:" + positionSimpleVO);
        positionSearchRepository.save(positionSimpleVO);

        // 存储 职位详情到redis
        PositionDetailVO positionDetailVO = new PositionDetailVO();
        BeanUtils.copyProperties(position, positionDetailVO);
        log.info("开始执行向redis中存储(修改)职位详情,positionId" + positionId + ".positiondetail:" + positionDetailVO);
        positionCacheRepository.saveDetailPositionState(positionId, positionDetailVO);
    }


    /*

     */
    @Override
    public void updateInfoById(CurrentPrincipal currentPrincipal,
                               Long id,
                               PositionUpdateInfoParam positionUpdateInfoParam) {
        //修改
        Position position = new Position();
        BeanUtils.copyProperties(positionUpdateInfoParam, position);
        position.setId(id);
        int rows = positionRepository.updateInfoById(position);
        if (rows != 1) {
            String message = "修改职位基本信息失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }

        //存储 职位简单信息到es
        log.info("开始执行向es中删除简单职位id:" + id);
        positionSearchRepository.deleteById(id);
        //测试 检查删除es时候还能查出对应信息
        PositionSimpleVO positionSimpleVO = new PositionSimpleVO();
        BeanUtils.copyProperties(position, positionSimpleVO);
        log.info("开始执行向es中存储(修改)职位详情positionSimpleVO:" + positionSimpleVO);
        positionSearchRepository.save(positionSimpleVO);


        // 存储 职位详情到redis
        PositionDetailVO positionDetailVO = new PositionDetailVO();
        BeanUtils.copyProperties(position, positionDetailVO);
        log.info("开始执行向redis中存储(修改)职位详情,positionId" + id + ".positiondetail:" + positionDetailVO);
        positionCacheRepository.saveDetailPositionState(id, positionDetailVO);
    }


    /*

     */
    @Override
    public void deleteById(Long id) {
        //延迟双删
        log.info("删除redis数据ById:"+id);
        positionRepository.deleteById(id);
        positionCacheRepository.deleteDetailPositionState(id);
        positionRepository.deleteById(id);
        log.info("开始执行向es中删除简单职位id:" + id);
        positionSearchRepository.deleteById(id);
    }

    /*

     */
    @Override
    public PositionDetailVO getStandardById(Long id) {

        PositionDetailVO detailPositionState = positionCacheRepository.getDetailPositionState(id);

        if (detailPositionState == null) {

            PositionDetailVO positionDetailVO = positionRepository.getStandardById(id);
            if (positionDetailVO == null) {
                String message = "职位信息不存在,请稍后再尝试！";
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
            }

            positionCacheRepository.saveDetailPositionState(positionDetailVO.getId(), positionDetailVO);
            log.info("从数据库中查出数据" + positionDetailVO);
            return positionDetailVO;
        }
        log.info("从redis中查出数据:" + detailPositionState);
        return detailPositionState;


    }


    /*

     */
    @Override
    public PageData<PositionSimpleVO> list(Long userId, Integer pageNum, Integer pageSize) {
        return positionRepository.list(userId, pageNum, pageSize);
    }

    /*
        预热职位service方法
     */
    @Override
    public void rebuildSearch() {
        log.debug("开始处理【重建职业的搜索数据】的业务");
        positionSearchRepository.deleteAll();
        Integer pageNum = 1;
        Integer pageSize = 100;
        Integer maxPage;
        PageData<PositionSimpleVO> pageData;
        do {
            pageData = positionRepository.listSearch(pageNum, pageSize);
            maxPage = pageData.getMaxPage();
            positionSearchRepository.saveAll(pageData.getList());
            pageNum++;
        } while (pageNum <= maxPage);
    }


}
